// test2
package main

import (
	//"encoding/json"
	"fmt"
	"github.com/bitly/go-simplejson"
	"io/ioutil"
	"net/http"
	"os"
)

type RecInfo struct {
	Date        string
	ServerIp    string
	ServerName  string
	LoadInfo    map[string]interface{}
	MemInfo     map[string]interface{}
	TrafficInfo map[string]interface{}
	ConnInfo    map[string]interface{}
	DiskInfo    map[string]interface{}
}

func main() {
	str := "http://192.168.11.26:1218/?name=recinfo&opt=get"

	resp, err := http.Get(str) //使用get方法访问
	if err != nil {
		return
	}

	defer resp.Body.Close()
	body, err1 := ioutil.ReadAll(resp.Body) //读取流数据
	if err1 != nil {
		fmt.Println("body is null")
		os.Exit(1)
	}

	js, err := simplejson.NewJson(body)
	if err != nil {
		fmt.Println("body is null")
		os.Exit(1)
		//panic(err.Error())
	}

	//get data
	date, _ := js.Get("Date").String()
	serverip, _ := js.Get("ServerIp").String()
	servername, _ := js.Get("ServerName").String()
	load, _ := js.Get("LoadInfo").Get("load").String()
	mem, _ := js.Get("MemInfo").Get("mem").String()
	swap, _ := js.Get("MemInfo").Get("swap").String()
	tfaffic_in, _ := js.Get("TrafficInfo").Get("traffic_in").String()
	tfaffic_out, _ := js.Get("TrafficInfo").Get("traffic_out").String()

	fmt.Printf("date=%s\n", date)
	fmt.Printf("serverip=%s\n", serverip)
	fmt.Printf("servername=%s\n", servername)
	fmt.Printf("load=%s\n", load)

	fmt.Printf("mem=%s\n", mem)
	fmt.Printf("swap=%s\n", swap)
	fmt.Printf("tfaffic_in=%s\n", tfaffic_in)
	fmt.Printf("tfaffic_out=%s\n", tfaffic_out)

	var nodes = make(map[string]interface{})
	nodes, _ = js.Get("ConnInfo").Map()
	for key, va := range nodes {
		fmt.Printf("%s=%s\n", key, va)
	}

	var dnodes = make(map[string]interface{})
	dnodes, _ = js.Get("DiskInfo").Map()
	for key, va := range dnodes {
		fmt.Printf("%s=%s\n", key, va)
	}

}
